import { GeneraDataType } from "@/interface";
import SortDataHook from "./sortData";

export default function TableHook(state: GeneraDataType = {}, callBack?: () => void) {
  const { searchForm } = state;
  const { fieldData } = SortDataHook();

  const sortChangeHandle = ({ property, order, sortList }: { property: string; order: string; sortList: { field: string; order: string }[] }) => {
    console.log(sortList.map(({ field, order }: any) => ({ field, order })));
    // const reg = /([A-Z])/g;
    // searchForm.orderField = property.replace(reg, "_$1").toLowerCase();
    //多字段排序
    searchForm.orderByList = sortList.map(({ field, order }: { field: string; order: string }) => ({ sort: order, order: fieldData[field] }));

    searchForm.order = fieldData[property];

    if (order) searchForm.sort = order;
    else {
      searchForm.sort = null;
      searchForm.order = null;
    }
    callBack && callBack();
  };

  return {
    sortChangeHandle
  };
}
